package com.ehi.common.jersey.status;

import com.google.common.collect.ImmutableMap;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;


public class ResponseBuilder {

    public static Response ok(Object entity) {
        return Response.ok(entity).build();
    }

    public static Response okForSingleValue(String name, Object entity) {
        Map<String, Object> singleMap = ImmutableMap.of(name, entity);
        return Response.ok(singleMap).build();
    }

    public static Response badRequest(String message) {
        return Response.status(Response.Status.BAD_REQUEST)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.PARAMETER_INVALID, message)).type(MediaType.APPLICATION_JSON).build();
    }

    public static Response badRequest(String message, String errorType) {
        return Response.status(Response.Status.BAD_REQUEST)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.PARAMETER_INVALID, message, errorType)).type(MediaType.APPLICATION_JSON).build();
    }

    public static Response quoteException(String message) {
        return Response.status(Response.Status.BAD_REQUEST)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.QUOTE_EXCEPTION, message)).build();
    }

    public static Response forbidden(String message) {
        return Response.status(Response.Status.FORBIDDEN)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.FORBIDDEN, message)).type(MediaType.APPLICATION_JSON).build();
    }

    public static Response unauthorized(String message) {
        return Response.status(Response.Status.UNAUTHORIZED)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.UNAUTHORIZED, message)).type(MediaType.APPLICATION_JSON).build();
    }

    public static Response notAcceptable(String message, String errorType) {
        return Response.status(Response.Status.NOT_ACCEPTABLE)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.NOT_ACCEPTABLE, message, errorType)).type(MediaType.APPLICATION_JSON).build();
    }

    public static Response notFoundObject() {
        return Response.ok("{}").build();
    }

    public static Response notFoundArray() {
        return Response.ok("[]").build();
    }

    public static Response serverError(ExceptionMessage exceptionMessage) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
            .type(MediaType.APPLICATION_JSON)
            .entity(new ErrorMessageWrapper(exceptionMessage)).build();
    }

    public static Response conflict(String message) {
        return Response.status(Response.Status.CONFLICT)
            .type(MediaType.APPLICATION_JSON)
            .entity(ErrorMessageWrapper.buildErrorMessage(ResponseTypeCode.API_UPDATED, message)).build();
    }

    public static Response fromErrorMessageWrapper(ErrorMessageWrapper wrapper) {
        return Response.status(Response.Status.fromStatusCode(wrapper.getStatusCode()))
            .type(MediaType.APPLICATION_JSON)
            .entity(wrapper)
            .build();
    }
}
